<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$query = "SELECT count(*) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$max_armies = $row[ 'count(*)' ] * 2;
$query = "SELECT count(*) FROM army WHERE ruler_id = " . $_SESSION['ruler_id'];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$name = formatInput( $_POST[ 'army_name' ] );
$village_id = getPostValue( 'village' );
$turn = getCurrentTurn();
$total = $row[ 'count(*)' ];
if( $total >= $max_armies ) {
	$_SESSION[ 'create_army_status' ] = translate( "You can't create any more armies. (Maximum is 1 army per village)" );
	header( "Location: armies.php" );
}
elseif( $name == "" ) {
	$_SESSION[ 'create_army_status' ] = translate( "You must enter a valid name for an army." );
	$_SESSION[ 'form_army_village' ] = $village_id;
	header( "Location: armies.php" );
}
else {
	$query = "SELECT count(*) FROM army WHERE name = '" . $name . "'";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if( $row[ 'count(*)' ] == 1 ) {
		$_SESSION[ 'create_army_status' ] = translate( "An army with that name already exists." );
		$_SESSION[ 'form_army_name' ] = $name;
		if( strstr( $name, ' ' ) == TRUE ) {
			$parts = explode( ' ', $name );
			unset( $parts[0] );
			$name_base = implode( ' ', $parts );
			$query = "SELECT count(*) " .
					"FROM army " .
					"WHERE name LIKE '%$name_base'";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$max_count = 1000;
			$start_at = $row[ 'count(*)' ];
			$index = $start_at;
			$found = false;
			while( !$found && ( $index < $start_at + $max_count ) ) {
				if( $index == 0 ) {
					$add = '1st ';
				}
				elseif( $index == 1 ) { 
					$add = '2nd '; 
				}
				elseif( $index == 2 ) { 
					$add = '3rd '; 
				}
				elseif( $index > 2 ) { 
					if( $index > 19 && $index % 10 == 0 ) {
						$add = ( $index + 1 ) . 'st ';
					}
					elseif( $index > 19 && $index % 10 == 1 ) {
						$add = ( $index + 1 ) . 'nd ';
					}
					elseif( $index > 19 && $index % 10 == 2 ) {
						$add = ( $index + 1 ) . 'rd ';
					}
					else {
						$add = ( $index + 1 ) . 'th ';
					}
				}
				$query = "SELECT count(*) " .
						"FROM army " .
						"WHERE name LIKE '" . $add . "$name_base'";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				if( $row[ 'count(*)' ] == 0 ) {
					$found = true;
					$name = $add . $name_base;
				}
				$index ++;
			}
			if( $found ) {
				$_SESSION[ 'create_army_status' ] .= ' ' . translate( '(Army number increased, this one is available)' );
				$_SESSION[ 'form_army_name' ] = $name;
			}
		}
		$_SESSION[ 'form_army_village' ] = $village_id;
		header( "Location: armies.php" );
	}
	else {
		$query = "SELECT x, y " .
				"FROM village " .
				"JOIN village_buildings ON village_buildings.village_id = village.village_id " .
				"WHERE ruler_id = '" . $_SESSION[ 'ruler_id' ] . "' AND building_id = 22 AND village.village_id = '" . $village_id . "'";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'x' ] ) ) {
			$query = "INSERT INTO army ( name, ruler_id, x, y, actual_x, actual_y, arrived_turn ) " .
					"VALUES ( '$name', " . $_SESSION[ 'ruler_id' ] . ", " . $row[ 'x' ] . " , " . $row[ 'y' ] . ", " . $row[ 'x' ] . " , " . $row[ 'y' ] . ", $turn )";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			header( "Location: army.php?name=" . $name );
		}
		else {
			$_SESSION[ 'create_army_status' ] = "\"You cheekie bastard! Hahahahahahaha...Hahahahahaha.\"";
			header( "Location: armies.php" );
		}
	}
}
?>
